package com.apex.shared.starter.constant;

import lombok.Getter;

/**
 * HTTP 状态码
 * 遵循 RFC 7231 规范
 *
 * @author MoNaiHui
 */
@Getter
public enum HttpStatusCode {

    // --- 1xx 信息性状态码 ---
    CONTINUE(100, "继续"),
    SWITCHING_PROTOCOLS(101, "切换协议"),
    PROCESSING(102, "正在处理"),
    EARLY_HINTS(103, "预处理信息"),

    // --- 2xx 成功状态码 ---
    OK(200, "请求成功"),
    CREATED(201, "已创建"),
    ACCEPTED(202, "已接受"),
    NON_AUTHORITATIVE_INFORMATION(203, "非授权信息"),
    NO_CONTENT(204, "无内容"),
    RESET_CONTENT(205, "重置内容"),
    PARTIAL_CONTENT(206, "部分内容"),
    MULTI_STATUS(207, "多状态"),
    ALREADY_REPORTED(208, "已报告"),
    IM_USED(226, "使用IM"),

    // --- 3xx 重定向状态码 ---
    MULTIPLE_CHOICES(300, "多种选择"),
    MOVED_PERMANENTLY(301, "永久移动"),
    FOUND(302, "临时移动"),
    SEE_OTHER(303, "查看其他位置"),
    NOT_MODIFIED(304, "未修改"),
    USE_PROXY(305, "使用代理"),
    TEMPORARY_REDIRECT(307, "临时重定向"),
    PERMANENT_REDIRECT(308, "永久重定向"),

    // --- 4xx 客户端错误状态码 ---
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权"),
    PAYMENT_REQUIRED(402, "需要付款"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不允许"),
    NOT_ACCEPTABLE(406, "不接受的请求"),
    PROXY_AUTHENTICATION_REQUIRED(407, "需要代理认证"),
    REQUEST_TIMEOUT(408, "请求超时"),
    CONFLICT(409, "资源冲突"),
    GONE(410, "资源已删除"),
    LENGTH_REQUIRED(411, "需要内容长度"),
    PRECONDITION_FAILED(412, "前提条件失败"),
    PAYLOAD_TOO_LARGE(413, "请求实体过大"),
    URI_TOO_LONG(414, "请求地址过长"),
    UNSUPPORTED_MEDIA_TYPE(415, "不支持的媒体类型"),
    RANGE_NOT_SATISFIABLE(416, "请求范围不符合要求"),
    EXPECTATION_FAILED(417, "预期结果失败"),
    IM_A_TEAPOT(418, "我是茶壶"),
    UNPROCESSABLE_ENTITY(422, "无法处理的实体"),
    LOCKED(423, "资源被锁定"),
    FAILED_DEPENDENCY(424, "依赖关系失败"),
    TOO_EARLY(425, "请求过早"),
    UPGRADE_REQUIRED(426, "需要升级"),
    PRECONDITION_REQUIRED(428, "需要前提条件"),
    TOO_MANY_REQUESTS(429, "请求过于频繁"),
    REQUEST_HEADER_FIELDS_TOO_LARGE(431, "请求头字段过大"),
    UNAVAILABLE_FOR_LEGAL_REASONS(451, "因法律原因不可用"),

    // --- 5xx 服务器错误状态码 ---
    INTERNAL_SERVER_ERROR(500, "服务器内部错误"),
    NOT_IMPLEMENTED(501, "未实现"),
    BAD_GATEWAY(502, "网关错误"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),
    HTTP_VERSION_NOT_SUPPORTED(505, "HTTP版本不支持"),
    VARIANT_ALSO_NEGOTIATES(506, "变体也可协商"),
    INSUFFICIENT_STORAGE(507, "存储空间不足"),
    LOOP_DETECTED(508, "检测到循环"),
    BANDWIDTH_LIMIT_EXCEEDED(509, "带宽限制超出"),
    NOT_EXTENDED(510, "未扩展"),
    NETWORK_AUTHENTICATION_REQUIRED(511, "需要网络认证");

    private final int value;
    private final String reasonPhrase;

    HttpStatusCode(int value, String reasonPhrase) {
        this.value = value;
        this.reasonPhrase = reasonPhrase;
    }

    /**
     * 判断是否为客户端错误
     */
    public boolean isClientError() {
        return value >= 400 && value < 500;
    }

    /**
     * 判断是否为服务器错误
     */
    public boolean isServerError() {
        return value >= 500;
    }

    /**
     * 判断是否为错误状态
     */
    public boolean isError() {
        return isClientError() || isServerError();
    }

    /**
     * 判断是否为成功状态
     */
    public boolean isSuccess() {
        return value >= 200 && value < 300;
    }

    /**
     * 根据状态码获取对应的枚举
     */
    public static HttpStatusCode valueOf(int statusCode) {
        for (HttpStatusCode code : values()) {
            if (code.value == statusCode) {
                return code;
            }
        }
        throw new IllegalArgumentException("无效的状态码: " + statusCode);
    }
} 